/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import { accountRegister, accountModify, getCaptcha } from '../services/user';
import { setAuthority } from '../utils/authority';
import { reloadAuthorized } from '../utils/Authorized';

const noop = () => {};
export default {
  namespace: 'register',

  state: {
    status: undefined,
  },

  effects: {
    // 注册
    *submit({ payload, then = noop, currentAction }, { call, put }) {
      const response = yield call(
        currentAction === 'register' ? accountRegister : accountModify,
        payload
      );
      if (!response) return;
      const { code, msg } = response;
      if (code > 0) {
        message.error(msg);
        return;
      }
      yield put({
        type: 'registerHandle',
        payload: response,
      });
      message.success(currentAction === 'register' ? '注册成功！' : '修改成功！');
      then();
    },
    *getCaptcha({ payload, __dva_resolve, then = noop }, { call }) {
      const response = yield call(getCaptcha, payload);
      if (!response) return;
      const { code, msg } = response;
      __dva_resolve(response)
      if (code > 0) {
        message.error(msg);
        return;
      }
      message.success(msg);
      then();
    },
  },

  reducers: {
    registerHandle(state, { payload }) {
      reloadAuthorized();
      return {
        ...state,
        status: payload.status,
      };
    },
  },
};
